package com.example.harmans;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.harmans.GPSTracker;
//import com.google.android.gms.maps.model.LatLng;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class FeesActivity extends ListActivity {

	String content;
	ListView lv;
	public String[][] stringArray;
	public String holdArray;
	public String courtString;
	public String[] court;
	ArrayList<Record> detailsArray = new ArrayList<Record>();
	String result = null;
	GPSTracker gps;

	private ArrayList<Map> data;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_court);
		new getinternetData().execute("");

		ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Find a Court");
        actionBar.setSubtitle("Court Fees");

        super.onCreate(savedInstanceState);
	}
	
	protected void otherfunc(String[][] courts) {

        DecimalFormat df = new DecimalFormat("#.##");
        gps = new GPSTracker(FeesActivity.this);
    	double latitude = gps.getLatitude();
    	double longitude = gps.getLongitude();

		this.data = new ArrayList<Map>();
		for (int i = 0; i < courts.length; i++) {
			String link = courts[i][0];
			String description = courts[i][1];

			// add some objects into the array list
	        Map m = new HashMap();
	        if(link.length()>48)
	        	link = link.substring(0, 48)+"...";
	        m.put("link", link);
	        m.put("description", description);
	        
	        this.data.add(m);
	        
	        // use main.xml for the layout
	        setContentView(R.layout.main);

	        // setup the data adaptor
	        String[] from = {"link", "description"};
	        int[] to = {R.id.link, R.id.description};
	        SimpleAdapter adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) this.data, R.layout.single_list_court_fee, from, to);
	        
	        // specify the list adaptor
	        setListAdapter(adapter);
		}
        return;
	}
	
	public class getinternetData extends AsyncTask<String, Void, String>{
		
		private static final String TAG = "MainActivity";
	    @Override
	    protected String doInBackground(String... params) {
	        HttpClient httpclient = new DefaultHttpClient();
	        InputStream inputStream = null;
	        HttpGet httpget = new HttpGet("http://www.mtcodelab.com/Development/harmans/blog");
	        try{
	            HttpResponse response = httpclient.execute(httpget);
	            StatusLine statusLine = response.getStatusLine();
	            int statusCode = statusLine.getStatusCode();
	            if(statusCode == 200){
	                HttpEntity entity = response.getEntity();
	                inputStream = entity.getContent();
	                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
	                String line = null;
	                StringBuilder sb = new StringBuilder();
	                while ((line = reader.readLine()) != null) {
	                	sb.append(line + "\n");
	                }
	                result = sb.toString();
	            }else{
	                Log.d(TAG, "Couldn't connect");
	            }
	        }catch(Exception e){
	            Log.d(TAG, e.toString());
	        }
	        finally {
	            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
	        }
	        try {
				JSONArray array = new JSONArray(result);
				stringArray = new String[array.length()][6];
				for (int i = 0; i < array.length(); i++) {
					JSONObject row = array.getJSONObject(i);
					stringArray[i][0] = row.getString("link");
					stringArray[i][1] = row.getString("description");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
	        
			return null;
	    }

	    @Override
	    protected void onPostExecute(String result) {
	    	otherfunc(stringArray);
	    }   
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.fees, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			startActivity(new Intent(FeesActivity.this,MainActivity.class)); 
			return true;
		}
	    if(item.getItemId() == R.id.icon_calculator) {
	    	Intent calcIntent = new Intent(FeesActivity.this,CalculatorActivity.class);
	    	startActivity(calcIntent);
	    }
		if(item.getItemId() == R.id.menu_home) {	    	
			Intent mainIntent = new Intent(FeesActivity.this,MainActivity.class);
			startActivity(mainIntent);
	    }
	    if(item.getItemId() == R.id.menu_court) {
	    	Intent courtIntent = new Intent(FeesActivity.this,CourtActivity.class);
	    	startActivity(courtIntent);
	    }
	    if(item.getItemId() == R.id.menu_collection) {
	    	Intent collectionIntent = new Intent(FeesActivity.this,CollectionActivity.class);
	    	startActivity(collectionIntent);
	    }
	    if(item.getItemId() == R.id.menu_faq) {
	    	Intent faqIntent = new Intent(FeesActivity.this,FaqActivity.class);
	    	startActivity(faqIntent);
	    }
	    if(item.getItemId() == R.id.menu_contact) {
	    	Intent contactIntent = new Intent(FeesActivity.this,ContactActivity.class);
	    	startActivity(contactIntent);
	    }
	    if(item.getItemId() == R.id.menu_about) {
	    	Intent aboutIntent = new Intent(FeesActivity.this,AboutActivity.class);
	    	startActivity(aboutIntent);
	    }
	    return true;
	}

}
